import { reqGetGoodsInfo, addGetShopCart} from "@/api";
import {getUUID} from '@/utils/uuid_token'
const state = {
    goodsInfo:{},
    //游客身份
    uuid_token:getUUID()
};
const mutations = {
    GETGOODS(state,goodsInfo){
        state.goodsInfo=goodsInfo
    }
}
const actions = {
    //获取产品信息action
    async getGoods({commit},id){
        let result=await reqGetGoodsInfo(id);
        if(result.code==200){
            commit('GETGOODS',result.data)
        }
    },
    //将产品添加到购物车中
   async addUpdataShopCart({commit},{id,skuNum}){
    //加入购物车返回的解构
    //服务器没有返回其余数据，不需要存储数据
        let result=await addGetShopCart(id,skuNum);
        // console.log(result)
        //加入购物车成功
        if(result.code==200){
            return 'ok'
            //加入购物车失败
        }else{
            return Promise.reject(new Error('fail'))
        }
    }

};
//简化数据
const getters = {
    //state.goodsInfo 初始值{}  路径导航
    categroyView(state){
        return state.goodsInfo.categroyView ||{}
    },
    //产品信息
    skuInfo(state){
        return state.goodsInfo.skuInfo ||{}
    },
    //产品售卖属性
    skuSaleAttrValueList(state){
        return state.goodsInfo.skuSaleAttrValueList ||[]
    }
};
export default {
    namespace:true,
    state,
    mutations,
    actions,
    getters
}
